package cn.ava.basic.utils;

import cn.ava.basic.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @ClassName: CheckSmsCodeUtil
 * @Description: 短信校验工具类
 * @Author: Avavaava
 * @Date: 2022/7/11 9:29
 * @version: 23.53
 *
 */
public class CheckCodeUtil {

    /**
     * @Title: checkSmsCode
     * @Description: 短信校验代码
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/11 9:29
     * @param:
     * @return:
     */
    public static void checkSmsCode(RedisTemplate<Object,Object> redisTemplate,String businessKey,String phone,String smsCode) {
        // 从redis中获取到短信验证码并判断短信验证码是否过期
        String smsCodeKey = businessKey + phone;
        Object smsCodeValue = redisTemplate.opsForValue().get(smsCodeKey);
        // 判断是否存在，如果不存在就是没有或者过期
        if (smsCodeValue == null){
            throw new BusinessException("验证码不存在或已过期！请重新获取");
        }
        // 获取redis中的短信验证码
        String[] split = smsCodeValue.toString().split(":");
        String Code = split[0];
        // 判断验证码是否正确
        if (!Code.equalsIgnoreCase(smsCode)) {
            throw new BusinessException("验证码错误！请重新输入");
        }
    }

    /**
     * @Title: checkEmpty
     * @Description: 非空校验
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/11 11:00
     * @param:
     * @return:
     */
    public static void checkEmpty(Object... param) {
        // 校验所有非空参数
        for (Object s : param) {
            if (StringUtils.isBlank(s.toString())) {
                throw new BusinessException("参数异常或为空！请重新填写");
            }
        }
    }


}
